<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="表名称" prop="tableName">
        <el-select style="width: 300px"
          v-model="queryParams.tableName"
          placeholder="请选择表名称"
          clearable
          @change="tableSelect"
        >
          <el-option
            v-for="(item, index) in tableOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="columnName" label="列名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="columnComment" label="注释"></el-table-column>
        <el-table-column prop="ordinalPosition" label="列排序"></el-table-column>
        <el-table-column prop="columnDefault" label="默认值"></el-table-column>
        <el-table-column prop="isNullable" label="是否为空"></el-table-column>
        <el-table-column prop="dataType" label="数据类型"></el-table-column>
        <el-table-column prop="characterMaximumLength" label="字符长度"></el-table-column>
        <el-table-column prop="numericPrecision" label="数值精度"></el-table-column>
        <el-table-column prop="numericScale" label="小数精度"></el-table-column>
        <el-table-column prop="columnType" label="列类型"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="handleEditColumn(scope.row)"
            >修改字段</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <!-- 添加或修改数据字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body >
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="200px">
        <input v-model="form.tableName" type="hidden"/>
        <el-form-item label="字段名称" prop="columnName" >
          <el-input v-model="form.columnName" placeholder="请输入字段名称" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select
            v-model="form.dataType"
            placeholder="请选择数据类型"
          >
            <el-option
              v-for="dict in dict.type.mysql_column_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字符长度" prop="characterMaximumLength" >
          <el-input v-model="form.characterMaximumLength" placeholder="请输入字符长度" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="数值精度" prop="numericPrecision">
          <el-input v-model="form.numericPrecision" placeholder="请输入数值精度" style="width: 217px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMdlsTable, listMdlsColumn, updateColumn } from "@/api/tool/gen";
export default {
  dicts: ['mysql_column_type'],
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      tables: [],
      // 总条数
      total: 0,
      // 表数据
      dbTableList: [],
      //表名称
      tableOptions:[],
      // 弹出层标题
      title: "修改字段信息",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      },
      //表参数
      tableParams:{
        pageNum: 1,
        pageSize: 100
      },
      //表字段
      form:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getTableList()
  },
  methods: {
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tableName: null,
        columnName: null,
        dataType: null,
        characterMaximumLength: null,
        numericPrecision: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.tables = selection.map(item => item.tableName);
    },
    // 查询表数据
    getTableList() {
      listMdlsTable(this.tableParams).then(res => {
        if (res.code === 200) {
          let data = res.rows;
          let treeAry = [];
          for (let i in data) {
            let v = data[i];
            let node = {};
            node.label = v.tableName;
            node.value = v.tableName;
            treeAry.push(node);
          }
          this.tableOptions = treeAry;
        }
      });
    },
    //表名称变更
    tableSelect(val){
      this.queryParams.tableName = val;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      listMdlsColumn(this.queryParams).then(res => {
        if (res.code === 200) {
          this.dbTableList = res.rows;
          this.total = res.total;
        }
      });
    },
    getList(){
      listMdlsColumn(this.queryParams).then(res => {
        if (res.code === 200) {
          this.dbTableList = res.rows;
          this.total = res.total;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //表字段修改
    handleEditColumn(row){
      this.reset();
      this.form.tableName = row.tableName,
      this.form.columnName = row.columnName,
      this.form.dataType = row.dataType,
      this.form.characterMaximumLength = row.characterMaximumLength,
      this.form.numericPrecision = row.numericPrecision
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      updateColumn(this.form).then(response => {
        this.$modal.msgSuccess("变更成功");
        this.open = false;
        this.getList();
      });
    },
  }
};
</script>
